import datetime
import asyncio
import aiohttp
from services.server_integration.schemas.alarms import ServerAlarm, Alarm

async def send_alarm_to_server(alarm: Alarm):
    server_address = '178.176.102.216:5002'
    try:
        alarm = ServerAlarm.from_alarm(alarm, '31.173.127.204', '00:b0:33:ff:f8:00')
        uri = f'http://{server_address}/alarms/set-current-alarm/'
        async with aiohttp.request(
                'POST', uri,
                json=alarm.dict()
        ) as response:
            if response.status != 200:
                return False

    except Exception as e:  # TODO
        print(f'send_current_alarm_to_server: {e}')

    return True


#
# async def ping_server():
#     server_address = '10.16.0.140:5001'
#     try:
#         uri = f'http://{server_address}/api/ping/'
#         async with aiohttp.request('GET', uri,) as response:
#             if response.status == 200:
#                 return True
#
#     except Exception as e:  # TODO
#         pass
#
#     return False

async def main():
    for ms in range(1_000):
        print(
            await send_alarm_to_server(
                Alarm(
                    code=108, value=False, state=False,
                    time=int(datetime.datetime(2023, 11, 8, 12, 9, 18, ms).timestamp()),
                    end_time=int(datetime.datetime.utcnow().timestamp())
                )
            ),
            ms
        )

asyncio.run(main())


# import ctypes
# from multiprocessing import Pool
# from signal import signal, SIGSEGV
# import os
# from multiprocessing import Pool


# def print_results(result):
#     print("callback result: ** " + str(result))
#
#
# def raise_a_fault():
#     print("raise_a_fault, pid " + str(os.getpid()))
#     try:
#         return 0
#         # return ctypes.string_at(0, 4)
#     except Exception as inst:
#         print("The fault is " + str(inst))
#
#
# processes_pool = Pool(1)
#
# try:
#     result = processes_pool.apply(raise_a_fault)
#     print(result)
#     # result.get()
#
#     processes_pool.join()
#
# except Exception as inst:
#     print("The Out exception is " + str(inst))
#
# print("All Done!")

# def some():
#     return 10
#
#
# def call_function_isolate(function, *args, **kwargs):
#     pool = Pool(1)
#     result = pool.apply(function, args=args, kwds=kwargs)
#     pool.close()
#     pool.join()
#
#     return result
#
#
# print(call_function_isolate(int, '12', base=8))
# print(call_function_isolate(ctypes.string_at, 0))












